package service

import (
	"express/internal/dao"
	"express/internal/model"
	"express/pkg/app"
	"time"
)

type ParcelService struct {
}

func NewParcelService() *ParcelService {
	return &ParcelService{}
}

func (p *ParcelService) CreatRecord(param *model.SendParcelParam) error {
	id, _ := app.GenerateID()
	parcel := model.Parcel{
		UserFrom:   param.UserFrom,
		UserTo:     param.UserTo,
		PosFrom:    param.PosFrom,
		PosTo:      param.PosTo,
		StartTime:  time.Now(),
		ArriveTime: time.Now().Add(time.Hour * 24),
		Identifier: id,
		IsSinged:   false,
		TakeCode:   app.GenerateTakeCode(),
		IsDoorTake: param.IsDoorTake,
		Weight:     param.Weight,
		Company:    param.Company,
	}

	return dao.CreateRecord(parcel)
}

func (p *ParcelService) GetParcelByPhone(phone string) ([]model.GetParcelInfoResponse, error) {
	parcels, err := dao.GetRecorder(phone)
	if err != nil {
		return nil, err
	}
	var list []model.GetParcelInfoResponse
	for _, parcel := range parcels {
		list = append(list, model.GetParcelInfoResponse{
			SenderPhone: parcel.UserFrom[len(parcel.UserFrom)-4:],
			TakeCode:    parcel.TakeCode,
			ArriveTime:  parcel.ArriveTime.Format("2006-01-02 15:04:05"),
			Company:     parcel.Company,
		})
	}
	return list, nil
}

func (p *ParcelService) GetParcelBySender(phone string) ([]model.Parcel, error) {
	return dao.GetRecorderBySender(phone)
}
